﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace ShopOnline
{
    public partial class ProductInfo : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), ClientID, "if(window['addVerticalDivider'] != undefined)addVerticalDivider();", true);
            ((ProductMaster)this.Master).SetPageTitle("Product Information");
            if (!IsPostBack)
            {
                BannerKey = ShopOnline.BLL.BannerKey.ProductPages;
                if (!string.IsNullOrEmpty(Request.QueryString[PRD]))
                {
                    int id = int.Parse(GetProductID());
                    LoadProductID(id);
                    //LoadRelatedProducts();
                }
            }
        }

        protected void prdDetails_OnCartChanged(object sender, EventArgs e)
        {
            ((ProductMaster)this.Master).SetCartTitle(string.Format("You have <a href='Cart.aspx'>{0} item(s)</a> in your shopping cart.", BasePage.SelectedItems.RowCount));
        }


        string GetProductID()
        {
            string catparm = Request.QueryString[BasePage.PRD];
            int lastIndex = catparm.LastIndexOf("_");
            return catparm.Substring(lastIndex + 1);
        }


        string CategoryID
        {
            get { return string.IsNullOrEmpty((string)ViewState["Category"]) == false ? ViewState["Category"].ToString() : "0"; }
            set { ViewState["Category"] = value; }
        }

        int BrandID
        {
            get { return ViewState["Brand"] != null ? (int)ViewState["Brand"] : 0; }
            set { ViewState["Brand"] = value; }
        }

        protected void LoadProductID(int id)
        {
            View_WebProducts product = View_WebProducts.LoadProductByID(id);
            if (product.RowCount > 0)
            {
                prdDetails.Product = product;
                CategoryID = product.s_CategoryPath.Replace("/", ",").Trim(',');
                if (CategoryID == "")
                    CategoryID = "-1";
                BrandID = product.BrandID;

                LoadTags(id);

                productList.Rebind();
            }
        }

        private void LoadTags(int id)
        {
            ProductTags tag = ProductTags.GetTags(id);
            string html = "";
            for (int i = 0; i < tag.RowCount; i++)
            {
                html += string.Format("<a class='tag' href='Search.aspx?tag={0}'>{0}</a>", tag.s_Tag);
                tag.MoveNext();
            }

            litTags.Text = html == "" ? "n/a" : html;
        }

        protected void productList_OnNeedDataSource(object sender, EventArgs e)
        {
            string filter = CategoryID.ToString() == "" ? View_WebProducts.ColumnNames.BrandID + " = " + BrandID.ToString() : View_WebProducts.ColumnNames.CategoryID + " in (" + CategoryID.ToString() + ")";
            if (View_Customers.GetCurrentUser() == null || View_Customers.GetCurrentUser().DealerPrice == false)
                filter += " and " + View_WebProducts.ColumnNames.IsDealerOnly + " != 1";

            filter += " and " + View_WebProducts.ColumnNames.ID + " != " + GetProductID();

            View_WebProducts prods = new View_WebProducts();
            int count = prods.Search(filter, "ID desc", 0, 3);
            productList.DataSource = prods.DefaultView;

            divRelated.Visible = count > 0;
        }
        
        protected void btnTag_OnClick(object sender, EventArgs e)
        {
            int id = int.Parse(GetProductID());
            string[] tags = txtTag.Text.ToLower().Split(new string[] { " ", "," , ";" }, StringSplitOptions.RemoveEmptyEntries);
            for(int i = 0; i < tags.Length; i++)
                ProductTags.AddTag(id, tags[i]);
            LoadTags(id);
            txtTag.Text = "";
        }

        string GetCatID()
        {
            string catparm = Request.QueryString[CAT];
            int lastIndex = catparm.LastIndexOf("_");
            return catparm.Substring(lastIndex + 1);
        }

        protected void productList_OnCartChanged(object sender, EventArgs e)
        {
            ((ProductMaster)this.Master).SetCartTitle(string.Format("You have <a href='Cart.aspx'>{0} item(s)</a> in your shopping cart.", BasePage.SelectedItems.RowCount));
        }
    }
}